package com.tuozixuan.caffeine;

import java.util.concurrent.TimeUnit;

import org.checkerframework.checker.nullness.qual.NonNull;

import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.Expiry;
import com.github.benmanes.caffeine.cache.LoadingCache;

public class EvictionBaseTimeDemo {

	public static void main(String[] args) {
		
		// 基于固定的过期时间驱逐策略
		LoadingCache<String, String> cache = Caffeine.newBuilder().expireAfterAccess(5, TimeUnit.MINUTES).build(key -> "lisi");
		LoadingCache<String, String> cache1 = Caffeine.newBuilder().expireAfterWrite(5, TimeUnit.MINUTES).build(key -> "lisi");

		// 基于不同的过期驱逐策略
		Caffeine.newBuilder().expireAfter(new @NonNull Expiry<String, String>() {
			public long expireAfterCreate(String key, String graph, long currentTime) {
				// Use wall clock time, rather than nanotime, if from an external resource
//			        long seconds = graph.creationDate().plusHours(5)
//			            .minus(System.currentTimeMillis(), MILLIS)
//			            .toEpochSecond();
//			        return TimeUnit.SECONDS.toNanos(seconds);
				return currentTime;
			}

			public long expireAfterUpdate(String key, String graph, long currentTime, long currentDuration) {
				return currentDuration;
			}

			public long expireAfterRead(String key, String graph, long currentTime, long currentDuration) {
				return currentDuration;
			}
		});
		
	}

}
